<?php

/**
 * SocialEnginePro
 *
 * @category   Application_Extensions
 * @package    TV
 * @author     Azim
 */

/**
 * @category   Application_Extensions
 * @package    TV
 */
class Tv_Plugin_Menus {

    public function canCreateTv() {
        // Must be logged in
        $viewer = Engine_Api::_()->user()->getViewer();
        if (!$viewer || !$viewer->getIdentity()) {
            return false;
        }

        // Must be able to create albums
        if (!Engine_Api::_()->authorization()->isAllowed('tv', $viewer, 'create')) {
            return false;
        }

        return true;
    }

    public function canViewTv() {
        $viewer = Engine_Api::_()->user()->getViewer();

        // Must be able to view albums
        if (!Engine_Api::_()->authorization()->isAllowed('tv', $viewer, 'view')) {
            return false;
        }

        return true;
    }

    public function favouriteTv() {
        $viewer = Engine_Api::_()->user()->getViewer();

        // Must be able to view albums
        if (!$viewer->getIdentity()) {
            return false;
        }

        return true;
    }

    public function onMenuInitialize_TvOptionsInvite() {
        $subject = Engine_Api::_()->core()->getSubject();
        $viewer = Engine_Api::_()->user()->getViewer();
        if ($subject->getType() !== 'tv') {
            throw new Course_Model_Exception('Whoops, not a tv!');
        }
        if (!$viewer->getIdentity()) {
            return false;
        }

        if (!$subject->authorization()->isAllowed($viewer, 'invite')) {
            return false;
        }

        return array(
            'label' => 'Invite Members',
            'icon' => 'application/modules/Tv/externals/images/invite.png',
            'route' => 'tv_specific',
            'class' => 'smoothbox',
            'params' => array(
                'controller' => 'tv',
                'action' => 'invite',
                'tv_id' => $subject->getIdentity(),
                'format' => 'smoothbox'
            )
        );
    }

    public function onMenuInitialize_TvOptionsDelete() {
        $subject = Engine_Api::_()->core()->getSubject();
        $viewer = Engine_Api::_()->user()->getViewer();
        if ($subject->getType() !== 'tv') {
            throw new Course_Model_Exception('Whoops, not a tv!');
        }
        if (!$viewer->getIdentity()) {
            return false;
        }

        if (!$subject->authorization()->isAllowed($viewer, 'delete')) {
            return false;
        }

        return array(
            'label' => 'Delete TV',
            'icon' => 'application/modules/Tv/externals/images/delete.png',
            'route' => 'tv_specific',
            'class' => 'smoothbox',
            'params' => array(
                'controller' => 'tv',
                'action' => 'delete',
                'tv_id' => $subject->getIdentity(),
                'format' => 'smoothbox'
            )
        );
    }

    public function onMenuInitialize_TvOptionsEdit() {
        $subject = Engine_Api::_()->core()->getSubject();
        $viewer = Engine_Api::_()->user()->getViewer();
        if ($subject->getType() !== 'tv') {
            throw new Course_Model_Exception('Whoops, not a tv!');
        }
        if (!$viewer->getIdentity()) {
            return false;
        }

        if (!$subject->authorization()->isAllowed($viewer, 'edit')) {
            return false;
        }

        return array(
            'label' => 'Edit TV',
            'icon' => 'application/modules/Tv/externals/images/edit.png',
            'route' => 'tv_specific',
            'params' => array(
                'controller' => 'tv',
                'action' => 'edit',
                'tv_id' => $subject->getIdentity()
            )
        );
    }

    public function onMenuInitialize_TvOptionsFavourite() {
        $subject = Engine_Api::_()->core()->getSubject();
        $viewer = Engine_Api::_()->user()->getViewer();
        if ($subject->getType() !== 'tv') {
            throw new Course_Model_Exception('Whoops, not a tv!');
        }
        if (!$viewer->getIdentity()) {
            return false;
        }

        if (!$subject->authorization()->isAllowed($viewer, 'favourite')) {
            return false;
        }
        
        if(!$subject->isFavourite()){
        return array(
            'label' => 'Favourite',
            'icon' => 'application/modules/Tv/externals/images/favourite.png',
            'route' => 'tv_specific',
            'class' => 'smoothbox',
            'params' => array(
                'controller' => 'tv',
                'action' => 'favourite',
                'tv_id' => $subject->getIdentity(),
                'format' => 'smoothbox'
            )
        );
        }
        else{
          return array(
            'label' => 'Unset Favourite',
            'icon' => 'application/modules/Tv/externals/images/favourite.png',
            'route' => 'tv_specific',
            'class' => 'smoothbox',
            'params' => array(
                'controller' => 'tv',
                'action' => 'unsetfavourite',
                'tv_id' => $subject->getIdentity(),
                'format' => 'smoothbox'
            )
        );  
        }
    }

    public function onMenuInitialize_TvOptionsReport() {
        $subject = Engine_Api::_()->core()->getSubject();
        $viewer = Engine_Api::_()->user()->getViewer();
        if ($subject->getType() !== 'tv') {
            throw new Course_Model_Exception('Whoops, not a tv!');
        }
        if (!$viewer->getIdentity()) {
            return false;
        }


        return array(
            'label' => 'Report',
            'icon' => 'application/modules/Tv/externals/images/report.png',
            'class' => 'smoothbox',
            'route' => 'default',
            'params' => array(
                'module' => 'core',
                'controller' => 'report',
                'action' => 'create',
                'format' => 'smoothbox',
                'subject' => $subject->getGuid(),
            )
        );
    }

    public function onMenuInitialize_TvOptionsShare() {
        $subject = Engine_Api::_()->core()->getSubject();
        $viewer = Engine_Api::_()->user()->getViewer();
        if ($subject->getType() !== 'tv') {
            throw new Course_Model_Exception('Whoops, not a tv!');
        }
        if (!$viewer->getIdentity()) {
            return false;
        }

        return array(
            'label' => 'Share',
            'icon' => 'application/modules/Tv/externals/images/share.png',
            'class' => 'smoothbox',
            'route' => 'default',
            'params' => array(
                'module' => 'activity',
                'controller' => 'index',
                'action' => 'share',
                'format' => 'smoothbox',
                'type' => $subject->getType(),
                'id' => $subject->getIdentity(),
            )
        );
    }
    
    public function onMenuInitialize_TvOptionsFeatured() {
        $subject = Engine_Api::_()->core()->getSubject();
        $viewer = Engine_Api::_()->user()->getViewer();
        if ($subject->getType() !== 'tv') {
            throw new Course_Model_Exception('Whoops, not a tv!');
        }
        if (!$viewer->getIdentity()) {
            return false;
        }

        if (!$subject->authorization()->isAllowed($viewer, 'featured')) {
            return false;
        }
        
        if($subject->featured == 0){
        return array(
                'label' => 'Featured',
                'icon' => 'application/modules/Tv/externals/images/featured.png',
                'route' => 'tv_specific',
                'class' => 'smoothbox',
                'params' => array(
                    'controller' => 'tv',
                    'action' => 'featured',
                    'tv_id' => $subject->getIdentity(),
                    'format' => 'smoothbox'
                )
            );
        }
        else{
            return array(
            'label' => 'Unset featured',
            'icon' => 'application/modules/Tv/externals/images/featured.png',
            'route' => 'tv_specific',
            'class' => 'smoothbox',
            'params' => array(
                'controller' => 'tv',
                'action' => 'unsetfeatured',
                'tv_id' => $subject->getIdentity(),
                'format' => 'smoothbox'
            )
        ); 
        }
    }

}